package org.zkoss.service;

import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;
import org.zkoss.model.bean.Company;
import org.zkoss.model.dao.CompanyDAO;

public class CompanyManagerImpl implements CompanyManager {

	private CompanyDAO companyDAO;

	public CompanyDAO getCompanyDAO() {
		return companyDAO;
	}

	public void setCompanyDAO(CompanyDAO companyDAO) {
		this.companyDAO = companyDAO;
	}

	@Transactional
	public boolean save(Company company) {
		try {
			companyDAO.saveOrUpdate(company);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Transactional
	public boolean delete(Company company) {
		try {
			companyDAO.delete(company);
			return true;
		} catch (DataAccessException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	@Transactional(readOnly = true)
	public Company findById(Integer id) {
		try {
			return companyDAO.find(Company.class, id);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@Transactional(readOnly = true)
	public List<Company> getCompanyList() {
		try {
			return companyDAO.findAll(Company.class);
		} catch (DataAccessException e) {
			e.printStackTrace();
			return null;
		}
	}
}
